package com.hmy.pig.service.impl;




import com.alibaba.fastjson2.JSONObject;
import com.hmy.pig.domain.HmyPig;
import com.hmy.pig.mapper.PigMapper;
import com.hmy.pig.service.PigService;
import com.hmy.pig.service.QrCodeService;
import com.ruoyi.common.security.utils.PigUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**猪只 业务层 实现类
 * @author yyh
 */
@Service
public class PigServiceImpl implements PigService {


    @Resource
    private PigMapper pigMapper;

    @Autowired
    private QrCodeService qrCodeService;

    @PostConstruct
    public void initPigData(){
        loadingPigDataCache();
    }

    private void loadingPigDataCache() {
        List<HmyPig> list = pigMapper.selectAll();
        for (HmyPig pig : list) {
            PigUtils.setPigCache(pig.getPigCode(),pig);
        }

    }


    @Override
    public List<HmyPig> selectPigList(String pigCode,
                                      String phouseType,
                                      String pigBirthdayStart,
                                      String pigBirthdayEnd) {
        return pigMapper.selectPigList(pigCode,phouseType,pigBirthdayStart,pigBirthdayEnd);
    }

    @Override
    public List<HmyPig> selectAll() {
        List<Object> list = PigUtils.getPigCacheAll();
        List<HmyPig> pigList = new ArrayList<>();
        for (Object o : list) {
            pigList.add(JSONObject.parseObject(String.valueOf(o), HmyPig.class));;
        }
        return pigList;
    }

    @Override
    public HmyPig selectPigByPigCode(String pigCode) {
        return pigMapper.selectPigByPigCode(pigCode);
    }

    /**
     * @param hmyPig
     * @return int
     * @author ZHL
     * @date 2024/1/29 14:47
     * @description  新增猪只档案信息
     */
    @Override
    public int insertPig(HmyPig hmyPig) {
        qrCodeService.upload(hmyPig);
        //新增猪只信息
        int rows = pigMapper.insertPig(hmyPig);
        return rows;
    }
    /**
     * @param pigId
     * @return com.hmy.pig.domain.HmyPig
     * @author ZHL
     * @date 2024/1/30 10:02
     * @description  通过猪只耳号查询猪只信息
     */
    @Override
    public HmyPig selectPigByPigId(Long  pigId) {
        return pigMapper.selectPigByPigId(pigId);
    }

    /**
     * @param hmyPig
     * @return int
     * @author ZHL
     * @date 2024/1/30 10:22
     * @description  修改猪只档案信息
     */
    @Override
    public int updatePig(HmyPig hmyPig) {
        //qrCodeService.upload(hmyPig);
        int i = pigMapper.updatePig(hmyPig);
        if(i > 0){
            PigUtils.setPigCache(hmyPig.getPigCode(),hmyPig);
        }
        return i;
    }
    /**
     * @param pigId
     * @return int
     * @author ZHL
     * @date 2024/1/30 15:00
     * @description 通过PigId 删除 猪只档案信息
     *
     */
    @Override
    public int deletePigByPigId(Long pigId) {
        return pigMapper.deletePigByPigId(pigId);
    }
    /**
     * @param pigIds
     * @return int
     * @author ZHL
     * @date 2024/1/30 15:00
     * @description 批量删除猪只档案信息
     */
    @Override
    public int deletePigByPigIds(Long[] pigIds) {
        return pigMapper.deletePigByPigIds(pigIds);
    }

    @Override
    public int batchInsert(List<HmyPig> dataList) {
        return 0;
    }

    @Override
    public int selectPigNum(String pigType) {
        return pigMapper.selectPigNum(pigType);
    }


}
